package com.pomr.entity;

import com.pomr.model.sys.Effect;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @作者：Administrator
 * @时间：14:31 2018/9/6 0006
 * @用途：权限信息
 **/
public class EffectEntity {
    private Integer value;
    private String title;
    private String name;
    private List<EffectEntity> data;

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<EffectEntity> getData() {
        return data;
    }

    public void setData(List<EffectEntity> data) {
        this.data = data;
    }

    public static List<EffectEntity> instanceEffect(List<Effect> effect) throws Exception{
        List<EffectEntity> effectEntities = new ArrayList<>();
        for(Effect e : effect){
            EffectEntity effectEntity = new EffectEntity();
            effectEntity.setValue(e.getId());
            effectEntity.setTitle(e.getTitle());
            effectEntity.setName(e.getTitle());
            if(e.getChilds()!=null && !e.getChilds().isEmpty()){
                effectEntity.setData(getChilds(e.getChilds()));
            }else{
                effectEntity.setData(new ArrayList<EffectEntity>());
            }
            effectEntities.add(effectEntity);
        }
        return effectEntities;
    }

    private static List<EffectEntity> getChilds(Set<Effect> childs) throws Exception{
        List<EffectEntity> effectEntities = new ArrayList<>();
        for(Effect effect : childs){
            EffectEntity effectEntity = new EffectEntity();
            effectEntity.setValue(effect.getId());
            effectEntity.setTitle(effect.getTitle());
            effectEntity.setName(effect.getTitle());
            if(effect.getChilds()!=null && !effect.getChilds().isEmpty()){
                effectEntity.setData(getChilds(effect.getChilds()));
            }else{
                effectEntity.setData(new ArrayList<EffectEntity>());
            }
            effectEntities.add(effectEntity);
        }
        return effectEntities;
    }
}
